using System;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using MCMLXVII.BackEnd.Data;
using MCMLXVII.BackEnd.Data.SystemDatabase;

namespace MCMLXVII.BackEnd.Services.Core.BusinessActions
{
    public static class Tools
    {


        public static msgToolsInfo[] GetToolsList(msgVoidRequest req)
        {
            DSTools.ToolsDataTable dt = ToolsAndPermissions.GetTools();
            msgToolsInfo[] tbl = new msgToolsInfo[dt.Rows.Count];
            int i = 0;
            foreach (DSTools.ToolsRow row in dt.Rows)
            {
                tbl[i] = new msgToolsInfo(row.ToolID, row.ToolName, null);
                i++;
            }
            return tbl;
        }

        public static msgToolsInfo GetToolInfo(msgGenericItemRequest<string> req)
        { 
            DSTools.ToolsRow  dr = ToolsAndPermissions.GetTool(req.Item);
            DSTools.ToolsPermissionsWithDescDataTable dt = ToolsAndPermissions.GetToolPermissions(dr.ToolID);
            ToolPermissionInfo[] permissions = new ToolPermissionInfo[dt.Rows.Count];
            int i = 0;
            foreach (DSTools.ToolsPermissionsWithDescRow row in dt.Rows)
            {
                permissions[i] = new ToolPermissionInfo(row.PermissionID, row.PermissionName, (row.Required == "Y"));
                i++;
            }
            return new msgToolsInfo(dr.ToolID, dr.ToolName, permissions);            
        }


        public static msgPermissionInfo[] GetPermissionsList(msgVoidRequest req)
        {
            DSTools.PermissionsDataTable dt = ToolsAndPermissions.GetPermissions();
            msgPermissionInfo[] tbl = new msgPermissionInfo[dt.Rows.Count];
            int i = 0;
            foreach (DSTools.PermissionsRow row in dt.Rows)
            {
                tbl[i] = new msgPermissionInfo(row.PermissionID, row.PermissionName, null);
                i++;
            }
            return tbl;
        }

        public static msgPermissionInfo GetPermissionInfo(msgGenericItemRequest<string> req)
        {
            DSTools.PermissionsRow dr = ToolsAndPermissions.GetPermission(req.Item);
            DSTools.PermissionToolsWithDescriptionDataTable dt = MCMLXVII.BackEnd.Data.SystemDatabase.ToolsAndPermissions.GetPermissionTools(dr.PermissionID);
            PermissionToolInfo[] tools = new PermissionToolInfo[dt.Rows.Count];
            int i = 0;
            foreach (DSTools.PermissionToolsWithDescriptionRow row in dt.Rows)
            {
                tools[i] = new PermissionToolInfo(row.ToolID, row.ToolName, (row.Required == "Y"));
                i++;
            }
            return new msgPermissionInfo(dr.PermissionID , dr.PermissionName, tools);

        }

        public static msgPermissionInfo[] GetMemberPermissionsList(msgGenericItemRequest<int> req)
        {
            DSTools.PermissionsDataTable dt = MCMLXVII.BackEnd.Data.SystemDatabase.ToolsAndPermissions.GetPermissions(req.Item);
            if (dt == null)
            {
                return null;
            }
            else
            {
                msgPermissionInfo[] tbl = new msgPermissionInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSTools.PermissionsRow row in dt.Rows)
                {
                    tbl[i] = new msgPermissionInfo(row.PermissionID, row.PermissionName, null);
                    i++;
                }
                return tbl;
            }
        }

        public static msgPermissionInfo[] GetMemberInheritedPermissionsList(msgGenericItemRequest<int> req)
        {
            DSTools.PermissionsDataTable dt = MCMLXVII.BackEnd.Data.SystemDatabase.ToolsAndPermissions.GetInheritedPermissions(req.Item);
            if (dt == null)
            {
                return null;
            }
            else
            {

                msgPermissionInfo[] tbl = new msgPermissionInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSTools.PermissionsRow row in dt.Rows)
                {
                    tbl[i] = new msgPermissionInfo(row.PermissionID, row.PermissionName, null);
                    i++;
                }
                return tbl;
            }
        }

        public static int UpdateMemberPermissions(msgMemberPermissionInfo req)
        { 
            int RequestingMemberID = Security.GetMemberIDFromSession();
            return ToolsAndPermissions.UpdateMemberPermissions(req.MemberID, RequestingMemberID, req.UpdateSequence, req.Permissions);
        
        }


    }
}
